package com.cloud.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.entity.ShopOrder;
import com.cloud.common.entity.ShopUser;
import com.cloud.common.feign.OrderService;
import com.cloud.common.feign.ProductService;
import com.cloud.user.dao.ShopUserDao;
import com.cloud.user.service.ShopUserService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 购物用户 服务实现类
 * </p>
 *
 * @author 李均华
 * @since 2021-06-04
 */
@Service
@Slf4j
public class ShopUserServiceImpl extends ServiceImpl<ShopUserDao, ShopUser> implements ShopUserService {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ProductService productService;

    @Override
    @GlobalTransactional
    public ShopOrder order(Integer id,Integer num) {
        log.info("用户下单，商品id=" + id + ",商品数量=" + num);
        ShopOrder order = orderService.order(id, num);
        Integer productRest = productService.reduceProduct(id, num);
        try {
            if (productRest < 0) {
                throw new RuntimeException("库存不足！");
            }
        } catch (RuntimeException e){
            e.printStackTrace();
        }
        return order;
    }
}
